home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / PRGMANIA / BFED.10 / BUFMAN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  5.5 KB  |  209 lines

  1. /********************************************
  2.     file: bufman.c
  3.     utility:
  4.     date: 1989
  5.     author: Jim Charlton
  6.     modifications:
  7.         1996: C. Moreau: 
  8.     comments: Each window has its own file buffer.  This file buffer is composed
  9.      of a linked list of blocks.  addmember() adds a new block to the end of the
  10.      list of blocks in the buffer, or puts the first block into the list if
  11.      there were no blocks in the list.  The size of the blocks in the buffer
  12.      is set with the globabl constant BLOCKSIZE.  Using a linked list of blocks
  13.      for a buffer simplifies memory management and speeds up inserts and deletes
  14.      since only the block containing the data in question need be modified.
  15.      insert_member() inserts a block into the linked list and splits the data
  16.      in the previous block between it and the new block.  This leaves two half
  17.      filled blocks for insertions.  dispose_member() deletes a block out of the
  18.      list....  happens when all of the data in a block is deleted or cut.
  19.      dispose_buf() will delete the entire file buffer by deleting all of its
  20.      blocks.
  21. *********************************************/
  22.  
  23. /********************************************
  24.     includes
  25. *********************************************/
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #include "e:\proging\c\libs\malib\alert.h"
  31. #include "bufman.h"
  32. #include "main.h"
  33. #include "menu.h"
  34. #include "wind.h"
  35.  
  36. #include "bfed_rsc.h"
  37.  
  38. /********************************************
  39.     defines
  40. *********************************************/
  41.  
  42. /********************************************
  43.     locals vars declarations & definitions
  44. *********************************************/
  45.  
  46. /********************************************
  47.     globals vars declarations
  48. *********************************************/
  49. char    *cutbuffer = ( (void*) 0L );
  50. long    cutlength = 0L;
  51.  
  52. /********************************************
  53.     locals functions declarations
  54. *********************************************/
  55.  
  56. /********************************************
  57.     globals functions definitions
  58. *********************************************/
  59.  
  60. /********************************************
  61.     locals functions definitions
  62. *********************************************/
  63. /*
  64.     name: addmember
  65.     utility: add a member to the linked list at the end.
  66.     comment: adds a new block to the end of the list of blocks in the buffer,
  67.      or puts the first block into the list if there were no blocks in the list.
  68.     parameters: 
  69.         windowptr thewin: ptr on window choosen.
  70.     return: linkbufptr: ptr on new member added.
  71.     date: 1989
  72.     author: Jim Charlton
  73.     modifications:
  74. */
  75. linkbufptr addmember(windowptr thewin)
  76. {   
  77.         /* allocate space for the new member */
  78.     linkbufptr newmem = (linkbufptr) malloc(sizeof(linkbuf));
  79.  
  80.     if (newmem)  /* if some memory available  */
  81.     {
  82.             /* initialize the newmem linkbuf */
  83.         newmem->next = NULL;
  84.         memset(newmem->block, '\0', (long) BLOCKSIZE);
  85.         newmem->inuse = 0;
  86.             /* insert into the linked list       */
  87.         if(!thewin->headptr)
  88.             thewin->headptr = newmem;
  89.         else
  90.         {
  91.             linkbufptr amem = thewin->headptr;
  92.         
  93.             while(amem->next)
  94.                 amem = amem->next;
  95.             amem->next = newmem;
  96.         } 
  97.     }
  98.     return(newmem);
  99. }
  100.  
  101. /*
  102.     name: insert_member
  103.     utility: insert a new linked buffer in the list for any list
  104.     comment: inserts a block into the linked list and splits the data
  105.      in the previous block between it and the new block.
  106.     parameters:
  107.         linkbufptr bufptr: ptr on block to insert.
  108.     return: int:
  109.     date: 1989
  110.     author: Jim Charlton
  111.     modifications:
  112. */
  113. int insert_member(linkbufptr bufptr)
  114. {
  115.         /* allocate space for the new member */
  116.     linkbufptr newmem = (linkbufptr) malloc(sizeof(linkbuf));
  117.  
  118.     if (newmem)  /* if some memory available  */
  119.     {
  120.             /* initialize the newmem linkbuf and insert into list */
  121.         int totransfer, left;    
  122.  
  123.         newmem->next = bufptr->next;
  124.         memset(newmem->block, '\0', (long)BLOCKSIZE);
  125.         newmem->inuse = 0L;
  126.         bufptr->next = newmem;
  127.     
  128.             /* now split the contents of the block in bufptr between old & new */
  129.         
  130.         totransfer = (int)(bufptr->inuse)/2;
  131.         left = (int)bufptr->inuse - totransfer;
  132.         memmove(newmem->block,bufptr->block+left,(long)totransfer);
  133.         newmem->inuse = totransfer;
  134.         bufptr->inuse = left;
  135.         return (0);
  136.     }
  137.     else
  138.         return(-1);
  139. }
  140.  
  141. /*
  142.     name: dispose_buf
  143.     utility: dispose of buffer
  144.     comment: 
  145.     parameters:
  146.     return: FALSE to signify user-abort
  147.     date: 1989
  148.     author: Jim Charlton
  149.     modifications:
  150. */
  151. void dispose_buf(windowptr thewin)
  152. {
  153.     if (!thewin->form)
  154.     {
  155.         linkbufptr    amem;
  156.  
  157.         if(thewin->changed)
  158.         {
  159.             if(rsc_falert(SAVE_CLOSE, thewin->title))
  160.                 do_menu(FILE,SAVE);
  161.         }      
  162.     
  163.         amem = thewin->headptr;
  164.         while(amem)
  165.         {
  166.             dispose_member(thewin,amem);
  167.             amem = thewin->headptr;        
  168.         }
  169.     }    
  170. }
  171.  
  172. /*
  173.     name: dispose_member
  174.     utility: 
  175.     comment: 
  176.     parameters:
  177.     return: 
  178.     date: 1989
  179.     author: Jim Charlton
  180.     modifications:
  181. */
  182. void dispose_member(windowptr thewin, linkbufptr memtodel)
  183. {
  184.     if(thewin->headptr)
  185.     {
  186.         if (thewin->headptr == memtodel)
  187.               thewin->headptr = thewin->headptr->next;
  188.           else
  189.         {
  190.             linkbufptr amem = thewin->headptr;
  191.         
  192.             while( (amem->next != memtodel) || !amem )
  193.             {
  194.                 amem=amem->next;
  195.             }
  196.                 /* search list 'til find memtodel in amem->next or amem==NULL */
  197.             if (amem)
  198.                 amem->next = amem->next->next;
  199.             else
  200.             {
  201.                 rsc_alert(MEMB_LIST);
  202.                 shutdown(2);
  203.             }      
  204.         }                       
  205.         free((char *) memtodel); /* free up space of deleted member  */
  206.     }             
  207. }                          
  208.  
  209.